﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LLog.Web.Common;
using LLog.Web.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace LLog.Web.Controllers
{
    [Produces("application/json")]
    [Route("api/Logs")]
    public class LogsController : Controller
    {
        private SqlSugarClient _client;
        public LogsController(SqlSugarClient client)
        {
            _client = client;
        }
        [HttpPost("list")]
        public PageList<Log> GetList(LogSearch search)
        {
            return _client.Queryable<Log>()
                        .WhereIF(!string.IsNullOrEmpty(search.AppName), x => x.AppName == search.AppName)
                        .WhereIF(search.BeginDate.HasValue, x => x.Timestamp >= search.BeginDate)
                        .WhereIF(search.EndDate.HasValue, x => x.Timestamp <= search.EndDate)
                        .OrderBy(x => x.Id, OrderByType.Desc)
                        .ToPage(search.PageIndex, search.PageSize);

        }

        [HttpPost]
        public void Create([FromBody]Log log)
        {
            _client.Insertable(log).ExecuteCommandAsync();
        }
    }
}